﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace FixedVehicle
{
    public class Biz
    {
        private static readonly Biz instance = new Biz();

        private Events.OnWriteMsgOut onMsg = null;

        private Events.OnFindFileInfo onFoundReport = null;

        public static Biz Instance
        {
            get
            {
                return instance;
            }
        }

        public Events.OnWriteMsgOut OnMsg
        {
            get
            {
                return onMsg;
            }

            set
            {
                onMsg = value;
            }
        }

        public string Filter
        {
            get
            {
                return filter;
            }

            set
            {
                filter = value;
            }
        }

        public List<FileInfo> FileList
        {
            get
            {
                return fileList;
            }

            set
            {
                fileList = value;
            }
        }

        public Events.OnFindFileInfo OnFoundReport
        {
            get
            {
                return onFoundReport;
            }

            set
            {
                onFoundReport = value;
            }
        }

        private Biz() { }

        private string filter = string.Empty;

        private List<FileInfo> fileList = new List<FileInfo>();

        public void FindFileList(string path)
        {
            try
            {
                FileList = new DirectoryInfo(path).GetFiles().ToList()
                            .FindAll(t => Regex.IsMatch(t.Extension, filter) && onFoundReport(t));
            }
            catch (Exception ex)
            {
                OnMsg(string.Format("执行查找文件过程中遇到错误：{0}", ex.Message));
            }
        }




    }

    public class Events
    {
        public delegate void OnWriteMsgOut(string msg);

        public delegate bool OnFindFileInfo(FileInfo fileInfo);
    }
}
